package com.wuhan.template.module.home.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wuhan.template.R;
import com.wuhan.template.module.home.entity.HotSearchEntity;

import java.util.List;

/**
 * Created by Administrator on 2016/5/13 0013.
 */
public class SearchRecyclerAdapter extends RecyclerView.Adapter<SearchRecyclerAdapter.ViewHolder> implements View.OnClickListener{

    private List<HotSearchEntity> hotSearchLists;
    private Context context;

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public SearchRecyclerAdapter(Context context, List<HotSearchEntity> hotSearchLists) {

        this.context = context;
        this.hotSearchLists = hotSearchLists;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view =LayoutInflater.from(context).inflate(R.layout.re_searh_list_item,null);
         ViewHolder holder =new ViewHolder(view);
        holder.title = (TextView) view.findViewById(R.id.search_title);
        holder.imageView = (ImageView) view.findViewById(R.id.searchImage);
        view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String title = hotSearchLists.get(position).getTitle();
        holder.title.setText(title);
        holder.itemView.setTag(title);

    }

    @Override
    public int getItemCount() {
        return hotSearchLists.size();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v,(String)v.getTag());
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        public ViewHolder(View itemView) {
            super(itemView);

        }
        ImageView imageView;
        TextView title;

    }

    //define interface
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view , String data);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
